package com.hzb.LNReader.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class ReaderPref {
	private static final String root = "root";
	private static final String fontSize = "fontSize";
	private static final String fontColor = "fontColor";
	private static final String screenNotTimeout = "screenNotTimeout";
	private static final String fullScreen = "fullScreen";
	
	private static final String bufferSize = "bufferSize";
	private static final String startReadLine = "startRead";
	private static final String bufferReadLine = "bufferRead";
	private static final String seekBackSize = "seekBackSize";
	
	private static final String viewHeight = "viewHeight";
	private static final String viewFill = "viewFill";
	
	private static final String rootDef = "/sdcard/sd/EBook";
	private static final String fontSizeDef = "24";
	private static final String fontColorDef = "FF404040";
	private static final int fontColorDefInt = 0xFF404040;
	
	private static final boolean screenNotTimeoutDef = true;
	private static final boolean fullScreenDef = true;
	
	private static final String bufferSizeDef = "40001";
	private static final String startReadDef = "40";
	private static final String bufferReadDef = "10000";
	private static final String seekBackSizeDef = "262144";
	
	private static final String viewHeightDef = "8000";
	private static final String viewFillDef = "60";

	public static int oldScreenTimeout;
	
	private static SharedPreferences prefs;
	
	public static void init(Context context) {
		prefs = PreferenceManager.getDefaultSharedPreferences(context);
	}
	public static String getRootDir() {
		return prefs.getString(root, rootDef);
	}
	
	private static int getInt(String key, String def) {
		try {
			return Integer.parseInt(prefs.getString(key, def));
		} catch (Exception e) {
			return Integer.parseInt(def);
		}
	}
	public static int getFontSize() {
		return getInt(fontSize, fontSizeDef);
	}
	
	public static int getFontColor() {
		String raw = prefs.getString(fontColor, fontColorDef);
		int ret = fontColorDefInt;
		try {
			ret = (int)Long.parseLong(raw, 16);
		} catch (Exception e) {
		}
		return ret;
	}
	
	public static boolean isFullScreen() {
		return prefs.getBoolean(fullScreen, fullScreenDef);
	}
	
	public static boolean isScreenNotTimeout() {
		return prefs.getBoolean(screenNotTimeout, screenNotTimeoutDef);
	}
	
	public static int getBufferSize() {
		return getInt(bufferSize, bufferSizeDef);
	}
	
	public static int getBufferReadLine() {
		return getInt(bufferReadLine, bufferReadDef);
	}
	
	public static int getStartReadLine() {
		return getInt(startReadLine, startReadDef);
	}
	
	public static int getSeekBackSize() {
		return getInt(seekBackSize, seekBackSizeDef);
	}
	
	public static int getViewHeight() {
		return getInt(viewHeight, viewHeightDef);
	}
	
	public static int getViewFillLine() {
		return getInt(viewFill, viewFillDef);
	}
	
	public static boolean setRootDir(String dir) {
		Editor editor = prefs.edit();
		editor.putString(root, dir);
		return editor.commit();
	}
	
	public static boolean setFullScreen(boolean on) {
		Editor editor = prefs.edit();
		editor.putBoolean(fullScreen, on);
		return editor.commit();
	}
}
